﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace SnippetPlugin.Forms {
    public partial class Options : Form {
        private Data.Config _config;
        Data.Snippet selectedSnippet;

        public Options() {
            InitializeComponent();
        }

        public Options(Data.Config config)
            : this() {
            _config = config;
            BindConfig();
        }

        private void BindConfig(){
            lbSnippet.ValueMember = "Id";
            lbSnippet.DisplayMember = "Name";
            lbSnippet.DataSource = _config.Snippets;
        }

        private void BindSnippet() {
            tbName.DataBindings.Clear();
            tbContent.DataBindings.Clear();
            if(lbSnippet.SelectedValue != null) {
                selectedSnippet = _config.Snippets.First(s => s.Id == (Guid)lbSnippet.SelectedValue);
                tbName.DataBindings.Add("Text", selectedSnippet, "Name");
                tbContent.DataBindings.Add("Text", selectedSnippet, "Content");
            } else {
                tbName.Text = "";
                tbContent.Text = "";
                selectedSnippet = null;
            }
            tbName.Focus();
        }

        private void SaveAndClose() {
            DialogResult = DialogResult.OK;
        }

        private void AddSnippet() {
            Data.Snippet snippet = new SnippetPlugin.Data.Snippet() { Id = Guid.NewGuid() };
            _config.Snippets.Add(snippet);
            lbSnippet.BindingContext[_config.Snippets].SuspendBinding();
            lbSnippet.BindingContext[_config.Snippets].ResumeBinding();
            lbSnippet.SelectedValue = snippet.Id;
            BindSnippet();
        }

        private void UpdateSnippet() {
            tbName.BindingContext[selectedSnippet].EndCurrentEdit();
            lbSnippet.BindingContext[_config.Snippets].SuspendBinding();
            lbSnippet.BindingContext[_config.Snippets].ResumeBinding();
            lbSnippet.SelectedValue = selectedSnippet.Id;
        }

        private void RemoveSnippet() {
            int oldIndex = lbSnippet.SelectedIndex;
            _config.Snippets.Remove(selectedSnippet);
            lbSnippet.BindingContext[_config.Snippets].SuspendBinding();
            lbSnippet.BindingContext[_config.Snippets].ResumeBinding();
            if(lbSnippet.Items.Count > oldIndex) {
                lbSnippet.SelectedIndex = oldIndex;
            } else {
                lbSnippet.SelectedIndex = oldIndex - 1;
            }
            BindSnippet();
        }

        private void bSave_Click(object sender, EventArgs e) {
            SaveAndClose();
        }

        private void bAdd_Click(object sender, EventArgs e) {
            AddSnippet();
        }

        private void lbSnippet_SelectedIndexChanged(object sender, EventArgs e) {
            if(lbSnippet.SelectedValue != null) {
                BindSnippet();
            }
        }

        private void tbName_KeyUp(object sender, KeyEventArgs e) {
            UpdateSnippet();
        }

        private void bRemove_Click(object sender, EventArgs e) {
            RemoveSnippet();
        }
    }
}
